﻿using Adaptive.Agrona;
using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.Extensions.Logging;
using Unity.Commons;
using Unity.Connections;
using Unity.Credentials;
using Unity.Logger;

namespace Unity.Debug
{
    public class ConsoleUnityProducer : IUnityProducer
    {
        private static readonly ILogger Logger = 
            UnityLogger.ConsoleLoggerFactory.CreateLogger(nameof(ConsoleUnityProducer));

        private readonly IUnityCredentials _credentials;

        private int _batchingCounter = 0;

        public ConsoleUnityProducer(IUnityCredentials credentials)
        {
            _credentials = credentials;
        }
        
        public bool AddToBatching(IDirectBuffer buffer, int offset, int length)
        {
            Logger.LogTrace("Call method AddToBatching");
            Logger.LogDebug("Add message to batching with offset = {Offset}, length = {Length}, buffer = {Buffer}", 
                offset, length, buffer);
            _batchingCounter++;
            return true;
        }

        public void Close()
        {
            Logger.LogTrace("Call method Close");
        }

        public int CompleteBatching()
        {
            Logger.LogTrace("Call method CompleteBatching");
            int doneBatching = _batchingCounter;
            _batchingCounter = 0;
            //Logger.LogDebug("Complete batching {Size} messages", doneBatching);
            return doneBatching;
        }

        public bool Connect(IUnityCredentials credentials)
        {
            Logger.LogTrace("Call method Connect");
            return true;
        }

        public void Dispose()
        {
            Logger.LogTrace("Call method Dispose");
        }

        public bool Produce(IDirectBuffer buffer, int offset, int length)
        {
            Logger.LogTrace("Call method Produce");
            Logger.LogDebug("Produce message with offset = {Offset}, length = {Length}, buffer = {Buffer}", 
                offset, length, buffer);
            return true;
        }
    }
}
